﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace SLS.ExClassLib.Converters
{
    [ValueConversion(typeof(Visibility), typeof(bool))]
    public class BooleanToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        private bool IsHideOnly(List<string> parameters)
        {
            if (parameters.Contains("hideonly"))
                return true;

            else return false;
        }

        private bool IsInverse(List<string> parameters)
        {
            return parameters.Contains("inverse");
        }

        private List<string> GetParameters(object parameter)
        {
            List<string> pars = new List<string>();
            if (parameter != null)
                pars.AddRange(((string)parameter).ToLower().Split(','));

            return pars;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            List<string> pars = GetParameters(parameter);
            bool isHideOnly = IsHideOnly(pars);

            if (value is bool)
            {
                bool val = (bool)value;
                if (IsInverse(pars))
                    value = !val;

                if ((bool)val)
                    return Visibility.Collapsed;
                return isHideOnly ? Visibility.Hidden : Visibility.Visible;
            }
            else if (value is Visibility)
                return ConvertBack(value, targetType, parameter, culture);

            return value;
        }

        private Visibility InverseVisibility(Visibility value, bool isHideOnly)
        {
            Visibility result = value;
            if (value == Visibility.Collapsed || (!isHideOnly && value == Visibility.Hidden))
                return Visibility.Visible;
            else
                return isHideOnly ? Visibility.Hidden : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            List<string> pars = GetParameters(parameter);
            bool isHideOnly = IsHideOnly(pars);

            if (value is Visibility)
            {
                Visibility v = (Visibility)value;
                if (IsInverse(pars))
                    v = InverseVisibility(v, isHideOnly);

                if (v == Visibility.Visible || (v == Visibility.Hidden && isHideOnly))
                    return true;
                return false;
            }
            else if (value is bool)
                return Convert(value, targetType, parameter, culture);

            return value;
        }

        #endregion
    } 

    [ValueConversion(typeof(bool), typeof(bool))]
    public class BooleanNegateConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        #endregion
    } 

    [ValueConversion(typeof(ImageSource), typeof(Image))]
    public class ImageSourceToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Image image = null;
            ImageSource imageSource = value as ImageSource;
            if (imageSource != null)
            {
                image = new Image();
                image.SnapsToDevicePixels = true;
                double size;
                if (parameter != null && double.TryParse((string)parameter, out size))
                    image.Width = image.Height = size;

                image.Source = imageSource;
            }
            return image;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
	/// <summary>
	/// Converter : Boolean to System.Windows.Visibility and not revert
	/// </summary>
	[ValueConversion(typeof(bool), typeof(System.Windows.Visibility))]
	public class BoolToVisibilityConverter : IValueConverter
	{
		/// <summary>
		/// IValueConverter.Convert
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			bool val = (bool)value;
			if (val)
			{
				return System.Windows.Visibility.Visible;
			}
			else
			{
				return System.Windows.Visibility.Hidden;
			}
		}

		/// <summary>
		/// IValueConverter.ConvertBack (not implemented)
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return false;
		}
	}


    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool flag = false;
            if (value is bool)
            {
                flag = (bool)value;
            }
            else if (value is bool?)
            {
                bool? nullable = (bool?)value;
                flag = nullable.Value;
            }
            return (flag ? Visibility.Collapsed : Visibility.Visible);

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((value is Visibility) && (((Visibility)value) == Visibility.Collapsed));

        }
    }
}
